using Android.App;
using Android.Content;
using Android.OS;
using Xamarin.ActionbarSherlockBinding.App;

namespace Cinemacenter.Droid
{
	public class CitiesDialogFragment : SherlockDialogFragment, IDialogInterfaceOnClickListener
	{
		ICitiesDialogFragmentListener listener;

		public interface ICitiesDialogFragmentListener
		{
			void OnNegativeButtonClick (IDialogInterface dialog, int id);

			void OnItemClick (IDialogInterface dialog, int item);
		}

		public static CitiesDialogFragment Instance (string[] csCities, int selectedIndex)
		{
			var frag = new CitiesDialogFragment ();
			var args = new Bundle ();
			args.PutCharSequenceArray ("cs_cities", csCities);
			args.PutInt ("selectedIndex", selectedIndex);
			frag.Arguments = args;
			return frag;
		}

		public override Dialog OnCreateDialog (Bundle savedInstanceState)
		{
			string[] cities = Arguments.GetCharSequenceArray ("cs_cities");
			int selectedIndex = Arguments.GetInt ("selectedIndex");

			var builder = new AlertDialog.Builder (Activity);
			builder.SetTitle (Resources.GetString (Resource.String.city_options_title));
			builder.SetCancelable (false);

			if (selectedIndex >= 0)
				builder.SetNegativeButton (Resources.GetString (Resource.String.city_options_cancel), this);
			builder.SetSingleChoiceItems (cities, selectedIndex, this);
			AlertDialog alert = builder.Create ();
			alert.SetCanceledOnTouchOutside (false);
			return alert;
		}

		public void OnClick (IDialogInterface dialog, int which)
		{
			if (listener != null) {
				switch (which) {
				case (int)DialogButtonType.Negative:
					listener.OnNegativeButtonClick (dialog, which);
					break;
				default:
					listener.OnItemClick (dialog, which);
					break;
				}
			}
		}

		public ICitiesDialogFragmentListener Listener {
			set {
				listener = value;
			}
		}
	}
}
